package com.cincc.cinccimdemo.common;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.LruCache;

import com.cincc.im.util.ImManager;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmojiUtils {

    private static final int CACHE_MAX_SIZE = 1024;
    private static LruCache<String, Bitmap> mDrawableCache;
    private static String[] names;
    private static List<Bitmap> bitmaps = new ArrayList<>();

    private static Pattern makePattern() {
        return Pattern.compile(patternOfDefault());
    }

    private static String patternOfDefault() {
        return "\\[[^\\[]{1,10}\\]";
    }

    public static void loadEmoji() {
        if (mDrawableCache == null) {
            mDrawableCache = new LruCache<String, Bitmap>(CACHE_MAX_SIZE) {
                @Override
                protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
                    if (oldValue != newValue)
                        oldValue.recycle();
                }
            };
            ImManager.getInstance().getWorkHandler().post(() -> {
                String[] imgs = MyUtils.getEmojiList(ImManager.getInstance().getApplicationContext());
                names = MyUtils.getEmojiTextList(ImManager.getInstance().getApplicationContext());

                for (int i = 0; i < imgs.length; i++) {
                    Bitmap bitmap = FileUtils.stringToBitmap(imgs[i]);
                    String name = names[i];
                    bitmaps.add(bitmap);
                    mDrawableCache.put(name, bitmap);
                }
            });
        }
    }

    public static String getName(int index) {
        return names[index];
    }

    public static List<Bitmap> getmDrawableCache() {
        return bitmaps;
    }

    public static SpannableString text2Emoji(Context context, final String source, float textSize) {
        SpannableString spannableString = new SpannableString(source);
        String regexEmotion = "\\[([\u4e00-\u9fa5\\w])+\\]";
        Pattern patternEmotion = Pattern.compile(regexEmotion);
        Matcher matcherEmotion = patternEmotion.matcher(spannableString);
        while (matcherEmotion.find()) {
            // 获取匹配到的具体字符
            String key = matcherEmotion.group();
            // 匹配字符串的开始位置
            int start = matcherEmotion.start();
            int size = (int) (textSize * 13.0f / 10.0f);
            Bitmap scaleBitmap = mDrawableCache.get(key);
            Bitmap bitmap = Bitmap.createScaledBitmap(scaleBitmap, size, size, true);
            ImageSpan span = new ImageSpan(context, bitmap, ImageSpan.ALIGN_BOTTOM);
            spannableString.setSpan(span, start, start + key.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        return spannableString;
    }
}
